package com.test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

public class Compress {
    private Activity context;
    private String filePath;

    private final int COMPRESS_SIZE = 200;
    public Compress(Activity context, String filePath){
    	this.filePath = filePath;
    	this.context = context;
    }
    public String getCompressedJPGPath() throws Exception{
        return compressImage(getBitmap());
    }
    private Bitmap getBitmap(){
    	BitmapFactory.Options opt = new BitmapFactory.Options();

        opt.inJustDecodeBounds = true;
        Bitmap bm = BitmapFactory.decodeFile(filePath, opt);

        int picWidth = opt.outWidth;
        int picHeight = opt.outHeight;


        WindowManager windowManager = context.getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int screenWidth = display.getWidth();
        int screenHeight = display.getHeight();

        opt.inSampleSize = 1;

        if (picWidth > picHeight) {
            if (picWidth > screenWidth)
                opt.inSampleSize = picWidth / screenWidth;
        } else {
            if (picHeight > screenHeight)

                opt.inSampleSize = picHeight / screenHeight;
        }

        opt.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(filePath, opt);

        return bm;
    }
    private String compressImage(Bitmap image) {
    	try {
    		ByteArrayOutputStream baos = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
            int options = 100;
            Log.d("mabo", "first compress" + baos.toByteArray().length/1024);
            
            
        	while (baos.toByteArray().length/1024 > COMPRESS_SIZE){ // size : 200kb
        		Log.d("mabo",  "options: " + options);
        		if(options < 40){
        			//two large image
        			Toast toast=Toast.makeText(context,"two large image", Toast.LENGTH_SHORT); 
        			toast.show(); 
        			Log.d("mabo", "two large image");
        			return null;
        		}
        		options -= 10; 
        		baos.reset();
        		image.compress(Bitmap.CompressFormat.JPEG, options, baos);              
        	}
            
            String compressImagePath = "/sdcard/test.jpg";
            File compressImageFile= new File(compressImagePath);  
            try {
            	FileOutputStream out = new FileOutputStream(compressImageFile);  
            	if(image.compress(Bitmap.CompressFormat.JPEG, options, out)){  
            		out.flush();  
	                out.close();  
	                Log.d("mabo", "succ");
	                return compressImagePath;
	            }
            	else{
            		return null;
            	}
            }   	
            catch (Exception e1){
            	return null;
            }

    	}
    		
    	catch (Exception e) {
    		 e.printStackTrace();
             return null;
    	}
    }
}